<?php
/**
 * Changed by Dumitru Russu
 * User: user2
 * Date: 8/31/12
 * Time: 5:31 PM
 * To change this template use File | users | File Templates.
 */
class Page_Be_Accounts_Users extends Page
{
    /**
     * @var Db_Accounts_Users
     */
    protected $tableObject;
	function __construct() {
        $userId = get('id', 'int');
        $this->idTableObject = $userId;
        $this->actionsTableObject = array(
            'addEditAction',
            'saveAction'
        );
        $this->classTableObject = 'Db_Accounts_Users';
		parent::__construct();
	}

	public function defaultAction() {
        if (perm('USERS_EDIT')) {
			$this->addBreadcrumb(t(T_FORM_TITLE, 'add'),'?action=addEdit');
		}
		$allUsers = Db_Accounts_Users::getUsers();
		self::$TPL->assign('allUsers', $allUsers);
		self::$TPL->setBodyTemplate('list_users');
		return array(
			'type' => 'display'
		);
	}

	public function addEditAction() {
		perm('USERS_EDIT', '/accounts/users');
		if(!empty($this->tableObject->objectArray->id)) {
			$currentUser = Db_Accounts_Users::getUserById($this->idTableObject);
			self::$TPL->assign('currentUser',$currentUser);
			$this->addBreadcrumb(t(T_FORM_TITLE, 'edit') . ' &quot;' . $currentUser['u_fullname'].'&quot;', '', true);
		} else {
			$this->addBreadcrumb(t(T_FORM_TITLE, 'add'), '', true);
		}
		$allGroups = Db_Accounts_UserGroups::findAllArray();
		self::$TPL->assign('allGroups', $allGroups);
		self::$TPL->setBodyTemplate('add_edit_user');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction() {
		perm('USERS_EDIT', '/accounts/users');
		$groupId = post('ug_id', 'int');
		$userLogin = post('u_login', 'string');
		$userPassword = post('u_password', 'string');
		$userEmail = post('u_email', 'string');
		$userFullName = post('u_fullname', 'string');
		$userPasswordConfirm = post('u_password_confirm','string');
        $this->message = t(T_FORM_MSG, 'success');

        if(!empty($groupId) && !empty($userLogin) && !empty($userEmail) && !empty($userFullName)) {

            if(!strcmp($userPassword, $userPasswordConfirm) && !empty($userPassword) && !empty($userPasswordConfirm)) {
                $this->error = true;
                $this->message = t(T_FORM_MSG, 'password_mismatch');
                return array(
                    'type' => 'redirect',
                    'query' => false
                );
            }
            if ($this->tableObject->objectArray->id) {
                $this->tableObject->id = $this->idTableObject;
                $auxUser = $this->tableObject->getUserById($this->tableObject->id);
                $this->tableObject->u_password = (!empty($userPassword) ? md5(HASH.$userPassword.HASH) : $auxUser['u_password'] );
            } else {
                $this->tableObject->u_password= md5(HASH.$userPassword.HASH);
            }
            $this->tableObject->u_login = $userLogin;
            $this->tableObject->u_group_id = $groupId;
			$this->tableObject->u_email = $userEmail;
			$this->tableObject->u_fullname = $userFullName;
			$this->tableObject->u_skype = post('u_skype', 'string');

			if(!$this->tableObject->save()) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		} else {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'required_fields');
		}
        return array(
            'type' => 'redirect',
            'query' => false
        );
	}

	public function deleteAction() {
		perm('USERS_EDIT', '/accounts/users');
		$this->message = t(T_FORM_MSG, 'success');
			if(!Db_Accounts_Users::abstractDelete($this->idTableObject)) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}
}